//
//  TUWENTableViewCell.m
//  GOGOTravel
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "TUWENTableViewCell.h"

@interface TUWENTableViewCell ()

@property (nonatomic, assign) CGFloat realHeight;

@end

@implementation TUWENTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initCell];
    }
    return self;
}

- (void)initCell{
    self.imageView1 = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView1];
    [_imageView1 release];
    
    self.label = [[BaseLabel alloc] init];
    [self.contentView addSubview:_label];
    [_label release];
}

- (void)setModel:(deList *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    //获取当前cell高度
    CGRect frame = [self frame];
    
    
    if ([_model.photo_height integerValue] > 0 && [_model.photo_width integerValue] > 0) {
        
        //imageview的自定义高度
        [_imageView1 sd_setImageWithURL:[NSURL URLWithString:_model.photo] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
        _imageView1.layer.cornerRadius = 5;
        _imageView1.layer.masksToBounds = YES;
        //获取图片的真实高度
        CGFloat height = [_model.photo_height floatValue];
        CGFloat width = [_model.photo_width floatValue];
        //缩放后宽度固定为335
        CGFloat scale = width / ([UIScreen mainScreen].bounds.size.width - 40);
        _realHeight = height / scale;
        
        _imageView1.frame = CGRectMake(20, 10, [UIScreen mainScreen].bounds.size.width - 40, _realHeight);
        
    }
    
    //label的自定义高度
    _label.text = _model.text;
    NSString *str = _model.text;
    //设置行数 越大越好
    _label.numberOfLines = 100;
    
    //设置字体样式
    NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:att context:nil];
    
    _label.frame = CGRectMake(20, _realHeight + 20, [UIScreen mainScreen].bounds.size.width - 40, rect.size.height);
       //cell的frame等于
    frame.size.height = _realHeight + rect.size.height + 40;
    //赋给cell
    self.frame = frame;
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

- (void)dealloc{
    [_model release];
    [_imageView1 release];
    [_label release];
    [super dealloc];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
